import { createRouter, createWebHistory } from 'vue-router';

const routerBase = import.meta.env.VITE_PAY_API_BASE || 'https://z5y8edt27b1l.ngrok.xiaomiqiu123.top';
import RegisterView from '../views/RegisterView.vue';
import LoginView from '../views/LoginView.vue';

import AddressManagementView from '@/views/AddressManagementView.vue';
import OrderView from '@/views/OrderView.vue';

import HomeView from '@/views/HomeView.vue';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: HomeView
  },
   // const router = createRouter({
  //   history: createWebHistory(routerBase),
  //   routes: [
  {
    path: '/orders',
    name: 'orders',
    component: () => import('@/views/OrderView.vue'),
    props: route => ({ activeTab: route.query.tab || 'unpaid' })
  },
  {
    path: '/order/:orderId',
    name: 'order-detail',
    component: () => import('@/views/OrderDetailView.vue'),
    props: true,
    meta: { requiresAuth: true }
  },{
    path: '/home',
    name: 'Home',
    component: () => import('@/views/HomeView.vue'),
    props: route => ({ 
      shopId: route.query.shopId 
    })
  },
  {
    path: '/address-management',
    name: 'address-management',
    component: AddressManagementView,
    meta: { requiresAuth: true }
  },
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/HomeView.vue'),
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('@/views/CartView.vue'),
  },
  {
    path: '/category',
    name: 'category',
    component: () => import('@/views/CategoryView.vue'),
  },
  {
    path: '/product/:productId',
    name: 'product-detail',
    component: () => import('../views/ProductDetailView.vue'),
    props: true
  },
  {
    path: '/payment',
    name: 'payment',
    component: () => import('../views/PaymentView.vue'),
    props: (route) => ({
      orderId: route.query.orderId,
      productName: route.query.productName,
      productPrice: route.query.productPrice,
      totalAmount: route.query.totalAmount,
      quantity: route.query.quantity,
      productImage: route.query.productImage
    }),
    meta: { requiresAuth: true }
  },
  {
    path: '/payment/success',
    name: 'payment-success',
    component: () => import('../views/PaymentSuccessView.vue'),
    props: (route) => ({
      out_trade_no: route.query.out_trade_no,
      total_amount: route.query.total_amount,
      timestamp: route.query.timestamp
    }),
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'profile',
    component: () => import('@/views/ProfileView.vue'),
    children: [
      {
        path: 'register',
        name: 'profile.register',
        component: RegisterView,
      },
      {
        path: 'login',
        name: 'profile.login',
        component: LoginView,
      },
    ]
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/admin/login',
      name: 'AdminLogin',
      component: () => import('@/views/AdminLogin.vue')
    },
    ...routes,
  ],
  // Remove duplicate routes property
});

//全局路由守卫
router.beforeEach((to, from, next) => {
  // 支付完成后域名切换
  if (to.matched.some(record => record.path === '/orders') && window.location.host.includes('ngrok')) {
    const localDomain = 'http://localhost:5177';
    window.location.href = localDomain + to.fullPath;
    return;
  }
  next();
});

export default router;